<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是进程通信——进程通信概述的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV05-05-进程通信-01-进程通信概述">
<meta property="og:url" content="https://sumumm.github.io/post/11a113f5.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是进程通信——进程通信概述的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-05-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1-01-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E6%A6%82%E8%BF%B0/img/image-20220527200852533.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-05-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1-01-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E6%A6%82%E8%BF%B0/img/image-20220609155626842.png">
<meta property="article:published_time" content="2023-07-02T13:52:18.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.994Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV05-操作系统">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-05-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1-01-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E6%A6%82%E8%BF%B0/img/image-20220527200852533.png">


<link rel="canonical" href="https://sumumm.github.io/post/11a113f5.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/11a113f5.html","path":"post/11a113f5.html","title":"LV05-05-进程通信-01-进程通信概述"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV05-05-进程通信-01-进程通信概述 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">一、进程通信的概念</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E9%80%9A%E4%BF%A1%E6%9C%BA%E5%88%B6%E7%AE%80%E4%BB%8B"><span class="nav-text">二、通信机制简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E7%AE%A1%E9%81%93%E7%AE%80%E4%BB%8B"><span class="nav-text">三、管道简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E4%BF%A1%E5%8F%B7%E7%AE%80%E4%BB%8B"><span class="nav-text">四、信号简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81-System-V-IPC-%E7%AE%80%E4%BB%8B"><span class="nav-text">五、 System V IPC 简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4"><span class="nav-text">1. 相关命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-ipcs"><span class="nav-text">1.1 ipcs </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">1.1.1 命令说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.1.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-ipcmk"><span class="nav-text">1.2 ipcmk </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">1.2.1 命令说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-ipcrm"><span class="nav-text">1.3 ipcrm </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">1.3.1 命令说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.3.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-key-%E5%92%8C-ID"><span class="nav-text">2. key 和 ID </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E4%B8%80%E4%B8%AA%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.1 一个实例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-ID-%E5%80%BC"><span class="nav-text">2.2 ID 值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-key-%E5%80%BC"><span class="nav-text">2.3 key 值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-key-%E5%92%8C-ID-%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="nav-text">3. key 和 ID 的创建</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-key-%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="nav-text">3.1 key 的创建</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-ftok"><span class="nav-text">3.1.1 ftok() </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.1.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-ID-%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="nav-text">3.2 ID 的创建</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E7%9B%B8%E5%85%B3%E5%87%BD%E6%95%B0%E6%B1%87%E6%80%BB"><span class="nav-text">4. 相关函数汇总</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81POSIX-IPC-%E7%AE%80%E4%BB%8B"><span class="nav-text">六、POSIX IPC 简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-POSIX"><span class="nav-text">1. POSIX </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-POSIX-IPC"><span class="nav-text">2. POSIX IPC </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E7%9B%B8%E5%85%B3%E5%87%BD%E6%95%B0%E6%B1%87%E6%80%BB"><span class="nav-text">3. 相关函数汇总</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81-Socket%E7%AE%80%E4%BB%8B"><span class="nav-text">七、 Socket简介</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/11a113f5.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV05-05-进程通信-01-进程通信概述 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV05-05-进程通信-01-进程通信概述
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-02 21:52:18" itemprop="dateCreated datePublished" datetime="2023-07-02T21:52:18+08:00">2023-07-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">LV05-操作系统</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>4.6k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>17 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是进程通信——进程通信概述的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-05-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1-01-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E6%A6%82%E8%BF%B0/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150>---</td>        <td align="left"><a href="" target="_blank">---<i class="fa fa-external-link-alt"></i> </a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、进程通信的概念"><a href="#一、进程通信的概念" class="headerlink" title="一、进程通信的概念"></a><font size=3>一、进程通信的概念</font></h1><p>进程间通信（<code>interprocess communication</code>，简称<code>IPC</code>）指两个进程之间的通信。系统中的每一个进程都有各自的地址空间，并且相互独立、隔离，每个进程都处于自己的地址空间中。所以同一个进程的不同模块之间进行通信都是很简单的，例如不同的函数就可以通过全局变量实现通信。</p>
<p>两个不同的进程之间要进行通信通常是比较困难的，因为这两个进程处于不同的地址空间中；通常情况下，大部分的程序是不要考虑进程间通信的，因为我们所接触绝大部分程序都是单进程程序（可以有多个线程），对于一些复杂、大型的应用程序，则会根据实际需要将其设计成多进程程序，例如<code>GUI</code>、服务区应用程序等。</p>
<h1 id="二、通信机制简介"><a href="#二、通信机制简介" class="headerlink" title="二、通信机制简介"></a><font size=3>二、通信机制简介</font></h1><p>虽然不同的进程的用户地址空间都是独立的，一般是不能互相访问的，但内核空间是每个进程都共享的，所以进程之间要通信就需要通过内核才能完成。</p>
<p><code>Linux</code>内核提供了多种<code>IPC</code>机制，基本是从<code>UNIX</code>系统继承而来，而对<code>UNIX</code>发展做出重大贡献的两大主力<code>AT&amp;T</code>的贝尔实验室及<code>BSD</code>（加州大学伯克利分校的伯克利软件发布中心）在进程间通信方面的侧重点有所不同。前者对<code>UNIX</code>早期的进程间通信手段进行了系统的改进和扩充，形成了<code>System V IPC</code>，通信进程局限在单个计算机内；后者则跳过了该限制，形成了基于套接字（<code>Socket</code>，也就是网络）的进程间通信机制。<code>Linux</code>则把两者继承了下来：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-05-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1-01-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E6%A6%82%E8%BF%B0/img/image-20220527200852533.png" alt="image-20220527200852533" style="zoom:40%;" />

<p>所以在进程中的通信方式如下：</p>
<table>
    <tr><td align="center" width=150px>UNIX IPC</td><td align="left">管道、FIFO、信号</td></tr>
    <tr><td align="center" width=150px>System V IPC</td><td align="left">信号量、消息队列、共享内存</td></tr>
    <tr><td align="center" width=150px>POSIX IPC</td><td align="left">信号量、消息队列、共享内存</td></tr>
    <tr><td align="center" width=150px>Socket IPC</td><td align="left">基于Socket进程间通信</td></tr>
</table>

<p>将会在后边的几篇笔记中详细介绍常用的几种通信方式的实现。</p>
<h1 id="三、管道简介"><a href="#三、管道简介" class="headerlink" title="三、管道简介"></a><font size=3>三、管道简介</font></h1><p>管道包括匿名管道（<code>pipe</code>）以及命名管道（<code>FIFO</code>）。匿名管道可用于具有亲属关系进程间的通信；命名管道克服了匿名管道没有名称的限制，从而允许无亲属关系进程间的通信。详细的使用会在后边的笔记中说明。</p>
<h1 id="四、信号简介"><a href="#四、信号简介" class="headerlink" title="四、信号简介"></a><font size=3>四、信号简介</font></h1><p>信号是一种通知性的通信方式，当某种事件发生时，用于向接收进程发出通知，根据需要，接收进程可以对通知做出某种反应，也可以进行忽略或者执行默认操作。详细的使用会在后边的笔记中说明。</p>
<h1 id="五、-System-V-IPC-简介"><a href="#五、-System-V-IPC-简介" class="headerlink" title="五、 System V IPC 简介"></a><font size=3>五、 System V IPC 简介</font></h1><p><code>System V IPC</code>包括了三种不同的进程通信机制：信号量、共享内存和消息队列。这三种会在后边的笔记中详细说明。</p>
<p>这里先来了解一下<code>System V</code>， 曾经也被称为 <code>AT&amp;T System V</code>，是<code>UNIX</code>操作系统众多版本中的一支。它最初由 <code>AT&amp;T</code> 开发，在<code>1983</code>年第一次发布。一共发行了<code>4</code>个<code>System V</code>的主要版本。其中<code>System V Release 4</code>，或者称为<code>SVR4</code>，是最成功的版本，成为一些<code>UNIX</code>共同特性的源头，例如 <code>SysV </code>初始化脚本(<code>/etc/init.d</code>)，用来控制系统启动和关闭，<code>System V Interface Definition</code> (<code>SVID</code>) 是一个<code>System V</code> 如何工作的标准定义。</p>
<p><strong>【注意】</strong></p>
<p>（1）每个<code>IPC</code>对象有唯一的<code>ID</code> 用<code>Key</code>关联。</p>
<p>（2）<code>IPC</code>对象创建后一直存在，直到被显式地删除。</p>
<h2 id="1-相关命令"><a href="#1-相关命令" class="headerlink" title="1. 相关命令"></a><font size=3>1. 相关命令</font></h2><h3 id="1-1-ipcs"><a href="#1-1-ipcs" class="headerlink" title="1.1 ipcs "></a><font size=3>1.1 ipcs </font></h3><h4 id="1-1-1-命令说明"><a href="#1-1-1-命令说明" class="headerlink" title="1.1.1 命令说明"></a><font size=3>1.1.1 命令说明</font></h4><p>查看信号量、共享内存和消息队列的信息。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ipcs [options]</span><br></pre></td></tr></table></figure>

<table>
    <tr><td align="center" width=150px>options</td><td align="center">说明</td></tr>
    <tr><td align="center" width=150px>-a</td><td align="left">显示全部可显示IPC对象的信息</td></tr>
    <tr><td align="center" width=150px>-q</td><td align="left">显示活动的消息队列信息</td></tr>
    <tr><td align="center" width=150px>-m</td><td align="left">显示活动的共享内存信息</td></tr>
    <tr><td align="center" width=150px>-s</td><td align="left">显示活动的信号量信息</td></tr>
    <tr><td align="center" width=150px>-l</td><td align="left">显示所有IPC对象的大小限制(小写的L)</td></tr>
</table>

<h4 id="1-1-2-使用实例"><a href="#1-1-2-使用实例" class="headerlink" title="1.1.2 使用实例"></a><font size=3>1.1.2 使用实例</font></h4><p>我们使用<code>ipcs -a</code>命令，将会在终端显示如下信息：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">--------- 消息队列 -----------</span><br><span class="line">键        msqid      拥有者  权限     已用字节数 消息      </span><br><span class="line"></span><br><span class="line">------------ 共享内存段 --------------</span><br><span class="line">键        shmid      拥有者  权限     字节     连接数  状态      </span><br><span class="line"></span><br><span class="line">--------- 信号量数组 -----------</span><br><span class="line">键        semid      拥有者  权限     nsems </span><br></pre></td></tr></table></figure>

<h3 id="1-2-ipcmk"><a href="#1-2-ipcmk" class="headerlink" title="1.2 ipcmk "></a><font size=3>1.2 ipcmk </font></h3><h4 id="1-2-1-命令说明"><a href="#1-2-1-命令说明" class="headerlink" title="1.2.1 命令说明"></a><font size=3>1.2.1 命令说明</font></h4><p>创建共享内存段、消息队列、信号量。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ipcmk [options]</span><br></pre></td></tr></table></figure>

<table>
    <tr><td align="center" width=150px>options</td><td align="center">说明</td></tr>
    <tr><td align="center" width=150px>-M</td><td align="left">创建大小为size字节的共享内存段。参数size之后可以是乘法后缀：KiB(=1024)，MiB(=1024*1024)，同理GiB等。(iB是可选的，例如，K与KiB同义）。或也可以后缀KB(=1000)，MB(=1000*1000)等。</td></tr>
    <tr><td align="center" width=150px>-Q</td><td align="left">创建一个消息队列。</td></tr>
    <tr><td align="center" width=150px>-S</td><td align="left">创建一个包含多个元素的信号量数组。</td></tr>
</table>

<h4 id="1-2-2-使用实例"><a href="#1-2-2-使用实例" class="headerlink" title="1.2.2 使用实例"></a><font size=3>1.2.2 使用实例</font></h4><p>我们使用以下命令创建三种<code>IPC</code>对象：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ipcmk -M 30</span><br><span class="line">ipcmk -Q 10</span><br><span class="line">ipcmk -S 20</span><br></pre></td></tr></table></figure>

<p>然后我们执行<code>ipcs -a</code>命令查看当前的<code>IPC</code>信息：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">--------- 消息队列 -----------</span><br><span class="line">键        msqid      拥有者  权限     已用字节数 消息      </span><br><span class="line">0xbf77197b 1          hk         644        0            0           </span><br><span class="line"></span><br><span class="line">------------ 共享内存段 --------------</span><br><span class="line">键        shmid      拥有者  权限     字节     连接数  状态      </span><br><span class="line">0x0db78bd3 5          hk         644        30         0                       </span><br><span class="line"></span><br><span class="line">--------- 信号量数组 -----------</span><br><span class="line">键        semid      拥有者  权限     nsems     </span><br><span class="line">0xb89eefb2 0          hk         644        20 </span><br></pre></td></tr></table></figure>

<h3 id="1-3-ipcrm"><a href="#1-3-ipcrm" class="headerlink" title="1.3 ipcrm "></a><font size=3>1.3 ipcrm </font></h3><h4 id="1-3-1-命令说明"><a href="#1-3-1-命令说明" class="headerlink" title="1.3.1 命令说明"></a><font size=3>1.3.1 命令说明</font></h4><p>移除一个消息对象，或共享内存段，或信号量，同时会将与<code>ipc</code>对象相关链的数据也一起移除。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ipcrm [options]</span><br><span class="line">ipcrm &#123;shm|msg|sem&#125; id...</span><br></pre></td></tr></table></figure>

<table>
    <tr><td align="center" width=150px>options</td><td align="center">说明</td></tr>
    <tr><td align="left" width=150px>-M shmkey</td><td align="left">移除用shmkey创建的共享内存段</td></tr>
    <tr><td align="left" width=150px>-m shmid </td><td align="left">移除用shmid标识的共享内存段</td></tr>
    <tr><td align="left" width=150px>-Q msgkey</td><td align="left">移除用msgkey创建的消息队列</td></tr>
    <tr><td align="left" width=150px>-q msqid </td><td align="left">移除用msqid标识的消息队列</td></tr>
    <tr><td align="left" width=150px>-S semkey</td><td align="left">移除用semkey创建的信号</td></tr>
    <tr><td align="left" width=150px>-s semid </td><td align="left">移除用semid标识的信号</td></tr>
</table>

<h4 id="1-3-2-使用实例"><a href="#1-3-2-使用实例" class="headerlink" title="1.3.2 使用实例"></a><font size=3>1.3.2 使用实例</font></h4><p>暂无。</p>
<h2 id="2-key-和-ID"><a href="#2-key-和-ID" class="headerlink" title="2. key 和 ID "></a><font size=3>2. key 和 ID </font></h2><h3 id="2-1-一个实例"><a href="#2-1-一个实例" class="headerlink" title="2.1 一个实例"></a><font size=3>2.1 一个实例</font></h3><p>使用<code>ipcs -M size</code>命令创建了三块共享内存区域，显示的信息如下所示：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">--------- 消息队列 -----------</span><br><span class="line">键        msqid      拥有者  权限     已用字节数 消息      </span><br><span class="line"></span><br><span class="line">------------ 共享内存段 --------------</span><br><span class="line">键        shmid      拥有者  权限     字节     连接数  状态      </span><br><span class="line">0x94e1fb75 6          hk         644        30         0                       </span><br><span class="line">0x473c57c6 7          hk         644        30         0                       </span><br><span class="line">0x9aacef52 8          hk         644        30         0                       </span><br><span class="line"></span><br><span class="line">--------- 信号量数组 -----------</span><br><span class="line">键        semid      拥有者  权限     nsems </span><br></pre></td></tr></table></figure>

<p>这里边包含了<code>key</code>值(键)、<code>ID</code>值（<code>msqid</code>、<code>shmid</code>、<code>semid</code>）、拥有者、权限等关键信息，实现两个进程之间通信的关键就在于<code>key</code>值和<code>ID</code>值。</p>
<h3 id="2-2-ID-值"><a href="#2-2-ID-值" class="headerlink" title="2.2 ID 值"></a><font size=3>2.2 ID 值</font></h3><p><code>Linux</code>系统为每个<code>IPC</code>机制都分配了唯一的<code>ID</code>，这个<code>ID</code>被称为<code>IPC</code>对象的<strong>标识符</strong>，所有针对<code>IPC</code>机制的操作都使用该<code>ID</code>值。<code>ID</code>值是在创建<code>IPC</code>对象的时候由系统分配的。</p>
<h3 id="2-3-key-值"><a href="#2-3-key-值" class="headerlink" title="2.3 key 值"></a><font size=3>2.3 key 值</font></h3><p>通信双方都需要通过某个方法来获取<code>ID</code>值。那怎么让两个进程获取到同一个<code>ID</code>值呢？<code>IPC</code>在实现时<strong>约定</strong>使用<code>key</code>值做为参数创建，如果在创建时使用相同的<code>key</code>值将得到同一个<code>IPC</code>对象的<code>ID</code>（即一方创建<code>ID</code>，另一方仅是获取<code>ID</code>），这样就保证了双方可以获取用于传递数据的同一个<code>IPC</code>机制<code>ID</code>值。这个<code>key</code>值被称为<code>System V IPC</code>外部标识符，又称为<code>IPC</code>键，它是一个<code>32</code>位的整型数据。</p>
<p>其实说到底，<code>key</code>的存在只是为了让两个进程可以对同一个<code>IPC</code>对象进行操作，<code>key</code>值有两种途径获取:</p>
<ul>
<li>途径一</li>
</ul>
<p><code>key</code>值可以人为的指定，也就是<code>key</code>为<code>IPC_PRIVATE</code>，但这样可能会造成同一个<code>IPC</code>机制有相同的<code>key</code>值。使用<code>IPC_PRIVATE</code>创建的<code>IPC</code>对象的<code>key</code>值属性为<code>0</code>，和<code>IPC</code>对象的<code>ID</code>就没有了对应关系。这样毫无关系的进程，就无法通过<code>key</code>值来得到<code>IPC</code>对象的<code>ID</code>（因为这种方式创建的<code>IPC</code>对象的<code>key</code>值都是<code>0</code>）。因此，这种方式产生的<code>IPC</code>对象，和无名管道类似，不能用于毫无关系的进程间通信。但也不是一点用处都没有，仍然可以用于有亲缘关系的进程间通信。</p>
<ul>
<li>途径二</li>
</ul>
<p><code>ftok()</code>可以创建<code>key</code>，该函数是<code>Linux</code>系统提供的系统调用。后边会有具体的说明。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/LV05-05-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1-01-%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1%E6%A6%82%E8%BF%B0/img/image-20220609155626842.png" alt="image-20220609155626842" style="zoom:50%;" />

<h2 id="3-key-和-ID-的创建"><a href="#3-key-和-ID-的创建" class="headerlink" title="3. key 和 ID 的创建"></a><font size=3>3. key 和 ID 的创建</font></h2><h3 id="3-1-key-的创建"><a href="#3-1-key-的创建" class="headerlink" title="3.1 key 的创建"></a><font size=3>3.1 key 的创建</font></h3><h4 id="3-1-1-ftok"><a href="#3-1-1-ftok" class="headerlink" title="3.1.1 ftok() "></a><font size=3>3.1.1 ftok() </font></h4><p>在<code>linux</code>下可以使用<code>man 3 ftok </code>命令查看该函数的帮助手册。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/ipc.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">key_t</span> <span class="title function_">ftok</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *pathname, <span class="type">int</span> proj_id)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>该函数用于创建一个<code>IPC</code>对象的<code>key</code>值。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li><code>pathname</code>：<code>char *</code>类型，是指定的文件名（可以包含路径），这个文件必须是存在的而且可以访问的。</li>
<li><code>proj_id</code>：<code>int</code>类型，该参数是子序号，它是一个整数，但是只有<code>8 bits</code>被使用，即实际范围是<code>0~255</code>。</li>
</ul>
<p><strong>【返回值】</strong><code>key_t</code>类型，当函数执行成功，则会返回<code>key_t</code>键值，失败返回<code>-1</code>。在一般的<code>UNIX</code>中，通常是将文件的索引节点（可以使用<code>ls -i</code>查看文件索引节点）取出，然后在前面加上子序号就得到<code>key_t</code>的值。</p>
<p><strong>【使用格式】</strong>一般情况下基本使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 需要包含的头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/ipc.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 至少应该有的语句 */</span></span><br><span class="line"><span class="type">key_t</span> key;</span><br><span class="line">key = ftok(<span class="string">&quot;./keytest.txt&quot;</span>, <span class="number">100</span>);</span><br></pre></td></tr></table></figure>

<p><strong>【注意事项】</strong></p>
<p>（1）若<code>pathname</code>指向的文件不存在，则会报如下错误：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">No such file or directory</span><br></pre></td></tr></table></figure>

<p>（2）在使用<code>ftok()</code>函数时，里面有两个参数，即<code>pathname</code>和<code>proj_id</code>，<code>pathname</code>为指定的文件名，而<code>proj_id</code>为子序列号，这个函数的返回值就是<code>key</code>，它与指定的文件的<strong>索引节点号</strong>和<strong>子序列号</strong>有关，如果文件的路径，名称和子序列号不变，那么得到的<code>key</code>值是否也是不变的呢？</p>
<p>如果<code>pathname</code>指向的文件或者目录<strong>被删除而且又重新创建</strong>，那么文件系统会赋予这个同名文件<strong>新的节点信息</strong>，所以键值<code>key</code>不一定相同。要确保<code>key</code>值不变，可以确保<code>ftok()</code>的文件不被删除，或者不用<code>ftok()</code>，指定一个固定的<code>key</code>值。</p>
<h4 id="3-1-2使用实例"><a href="#3-1-2使用实例" class="headerlink" title="3.1.2使用实例"></a><font size=3>3.1.2使用实例</font></h4><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span>   <span class="comment">/* perror */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/ipc.h&gt;</span> <span class="comment">/* ftok */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 主函数 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">key_t</span> key;</span><br><span class="line">	key = ftok(<span class="string">&quot;./keytest.txt&quot;</span>, <span class="number">100</span>);</span><br><span class="line">	<span class="keyword">if</span>(key &lt; <span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		perror(<span class="string">&quot;ftok&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;key=%x, sizeof(key)=%ld\n&quot;</span>, key, <span class="keyword">sizeof</span>(key));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure><p>在终端执行以下命令编译程序：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out </span><br><span class="line">./a.out # 执行可执行程序</span><br></pre></td></tr></table></figure><p>然后，终端会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">key=643d0b38, sizeof(key)=4</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2-ID-的创建"><a href="#3-2-ID-的创建" class="headerlink" title="3.2 ID 的创建"></a><font size=3>3.2 ID 的创建</font></h3><p><code>ID</code>的创建都是在创建<code>IPC</code>对象的时候分配的，后边的笔记中会有<code>IPC</code>对象的创建及操作，那里会有说明的。</p>
<h2 id="4-相关函数汇总"><a href="#4-相关函数汇总" class="headerlink" title="4. 相关函数汇总"></a><font size=3>4. 相关函数汇总</font></h2><table>
    <tr><td align="center" width=150px>IPC对象名称  </td><td align="center">消息队列          </td><td align="center">信号量           </td><td align="center">共享内存</td></tr>
    <tr><td align="left" width=150px>头文件       </td><td align="center">&lt;sys/msg.h&gt; </td><td align="center">&lt;sys/sem.h&gt;</td><td align="center">&lt;sys/shm.h&gt;</td></tr>
    <tr><td align="left" width=150px>关联数据结构 </td><td align="center">msqid_ds          </td><td align="center">semid_ds         </td><td align="center">shmid_ds</td></tr>
    <tr><td align="left" width=150px>创建或打开IPC</td><td align="center">msgget()	      </td><td align="center">semget()         </td><td align="center">shmget()</td></tr>
    <tr><td align="left" width=150px>IPC控制      </td><td align="center">msgctl()          </td><td align="center">semctl()         </td><td align="center">shmctl()</td></tr>
    <tr><td align="left" width=150px>IPC操作      </td><td align="center">msgsnd()、msgrcv()</td><td align="center">semop()          </td><td align="center">shmat()、shmdt()</td></tr>
</table>


<h1 id="六、POSIX-IPC-简介"><a href="#六、POSIX-IPC-简介" class="headerlink" title="六、POSIX IPC 简介"></a><font size=3>六、POSIX IPC 简介</font></h1><h2 id="1-POSIX"><a href="#1-POSIX" class="headerlink" title="1. POSIX "></a><font size=3>1. POSIX </font></h2><p><code>POSIX</code>(<code>Portable Operating System Interface for Computing Systems</code>)是由<code>IEEE</code> 和<code>ISO/IEC</code> 开发的一簇标准。该标准是基于现有的<code>UNIX</code> 实践和经验，描述了操作系统的调用服务接口，用于保证编制的应用程序可以在源代码一级上在多种操作系统上移植运行。</p>
<p>它是在<code>1980</code> 年早期一个<code>UNIX</code> 用户组(<code>usr/group</code>)的早期工作的基础上取得的。该<code>UNIX</code> 用户组原来试图将<code>AT&amp;T</code> 的系统<code>V</code> 和<code>Berkeley CSRG</code>的<code>BSD</code> 系统的调用接口之间的区别重新调和集成，从而于<code>1984</code> 年产生了<code>/usr/group</code> 标准。</p>
<p><code>1985</code> 年，<code>IEEE</code>操作系统技术委员会标准小组委员会(<code>TCOS-SS</code>)开始在<code>ANSI</code> 的支持下责成(指定)<code>IEEE</code> 标准委员会制定有关程序源代码可移植性操作系统服务接口正式标准。</p>
<p>到了<code>1986</code> 年<code>4</code> 月，<code>IEEE</code> 就制定出了试用标准。第一个正式标准是在<code>1988</code> 年<code>9</code> 月份批准的(<code>IEEE 1003.1-1988</code>)，也就经常提到的<code>POSIX.1</code> 标准。</p>
<h2 id="2-POSIX-IPC"><a href="#2-POSIX-IPC" class="headerlink" title="2. POSIX IPC "></a><font size=3>2. POSIX IPC </font></h2><p><code>POSIX IPC</code>也包含了消息队列、信号量和共享内存三种，但是<code>POSIX</code>的语法相对于<code>System V</code>会更加的简单些，使用名字代替键来标识<code>IPC</code>对象。</p>
<h2 id="3-相关函数汇总"><a href="#3-相关函数汇总" class="headerlink" title="3. 相关函数汇总"></a><font size=3>3. 相关函数汇总</font></h2><table>
    <tr><td align="left" width=150px>IPC对象</td><td align="center">消息队列</td><td align="center">信号量</td><td align="center">共享内存</td></tr>
    <tr><td align="left" width=150px>头文件</td><td align="center">&lt;mqueue.h&gt;</td><td align="center">&lt;semaphore.h&gt;</td><td align="center">&lt;sys/mman.h&gt;</td></tr>
    <tr><td align="left" width=150px>对象句柄</td><td align="center">mqd_t</td><td align="center">sem_t *</td><td align="center">int(文件描述符)</td></tr>
    <tr><td align="left" width=150px>创建或打开IPC</td><td align="center">mq_open()</td><td align="center">sem_open()</td><td align="center">shm_open()、mmap()</td></tr>
    <tr><td align="left" width=150px>关闭IPC</td><td align="center">mq_close()</td><td align="center">sem_close()</td><td align="center">munmap()</td></tr>
    <tr><td align="left" width=150px>断开与IPC的链接</td><td align="center">mq_unlink()</td><td align="center">sem__unlink()</td><td align="center">shm__unlink()</td></tr>
    <tr><td align="left" width=150px>IPC操作</td><td align="center">mq_send()、mq_receive()</td><td align="center">sem_post()、sem_wait()、sem_getvalue() </td><td align="center">在共享区域中的位置上操作</td></tr>
    <tr><td align="left" width=150px>其他操作</td><td align="center">mq_setattr()、mq_getattr()、mq_notify()</td><td align="center">sem_init()、sem_destroy()</td><td align="center">---</td></tr>
</table>


<h1 id="七、-Socket简介"><a href="#七、-Socket简介" class="headerlink" title="七、 Socket简介"></a><font size=3>七、 Socket简介</font></h1><p><code>socket</code>我们一般翻译为套接字，它主要是针对于网络编程时，实现进程的通信，后边网络编程部分的笔记会有详细的说明。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/11a113f5.html">LV05-05-进程通信-01-进程通信概述</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月02日 - 21:52</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/11a113f5.html" title="LV05-05-进程通信-01-进程通信概述">https://sumumm.github.io/post/11a113f5.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV05-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag"><i class="fa fa-tag"></i> LV05-操作系统</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/3873081f.html" rel="prev" title="LV05-05-进程通信-02-管道">
                  <i class="fa fa-angle-left"></i> LV05-05-进程通信-02-管道
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/2d62d275.html" rel="next" title="LV05-04-线程应用-01-线程池">
                  LV05-04-线程应用-01-线程池 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
